<?php
namespace app\system\controller;

use app\common\model\AreaModel;
use app\common\model\AuthRuleModel;
use app\common\model\CmsCategoryModel;
use app\common\model\CmsMoudleModel;
use app\common\model\ConfigModel;
use app\common\model\DictModel;
use app\common\model\ShopExpressModel;
use app\common\model\ShopGoodsBrandModel;
use app\common\model\ShopGoodsCategoryModel;
use app\common\model\ShopSiteGroupModel;
use app\common\model\UserModel;
use think\Db;

class IndexController extends BaseController
{
    /**
     * 初始化
     */
    protected function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 后台首页
     * @return mixed
     */
    public function index()
    {
        $userModel = new UserModel();
        $userinfo  = $userModel->get($this->getUid());
        $title     = '控制台';
        $ruleList  = $userModel->getMyRuleList($userinfo['group_id'], $this->getUtype());
        $this->assign('title', $title);
        $this->assign('userinfo', $userinfo);
        $this->assign('siteId', $this->getSiteId());
        $this->assign('ruleList', $ruleList);
        return $this->fetch();
    }

    /**
     * 右侧菜单
     */
    public function getMenu(){
        $userModel = new UserModel();
        $groupid = $this->request->param('groupid');
        $ruleid = $this->request->param('ruleid');
        $ruleList  = $userModel->getMyRuleList($groupid, $this->getUtype());
        $this->assign('ruleList',$ruleList[$ruleid]['children']);
        echo $this->fetch();
    }
    /**
     * 获取待办事项
     */
    public function getTodo(){
        $map['site_id'] = $this->getSiteId();
        $static['t1'] = Db::table('shop_order')->where($map)->where('pay_status=0 and order_status>=0')->count(); //未付款订单
        $static['t2'] = Db::table('shop_order')->where($map)->where('pay_status=1 and order_status=0')->count(); //待确认订单
        $static['t3'] = Db::table('shop_order')->where($map)->where('pay_status=1 and shipping_status=0')->count(); //代发货订单
        $static['t4'] = Db::table('shop_order_refund')->where($map)->where('refund_status=1')->count(); //退款申请单
        $static['t5'] = Db::table('shop_goods_geval')->where($map)->where('geval_status=0')->count(); //待处理订单评价
        $static['t6'] = Db::table('shop_order')->where($map)->where('order_status=2 and invoice=1')->count(); //待开发票
        $sum = 0;
        foreach ($static as $v){
            $num = empty($v)?0:$v;
            $sum += $num;
        }
        $static['total'] = $sum;
        return $static;
    }


    /**
     * 主窗口
     */
    public function main()
    {
        return $this->fetch();
    }

    /**
     * 初始化数据
     */
    public function initCache()
    {
        if ($this->getUtype() == 'system') {
            $areaModel        = new AreaModel();
            $cmsMoudleModel   = new CmsMoudleModel();
            $dictModel        = new DictModel();
            $shopExpressModel = new ShopExpressModel();
            $configModel        = new ConfigModel();
            $cmsCategoryModel   = new CmsCategoryModel();
            $goodsCategoryModel = new ShopGoodsCategoryModel();
            $areaModel->updateCache(); //更新地区缓存
            $arr[] = "地区缓存[area]更新完毕！";
            $cmsMoudleModel->updateCache(); //更新cms模型缓存
            $arr[] = "模型缓存[cms_moudle]、字段缓存[cms_field[1,2]]、推荐位缓存[cms_flag[1,2]]更新完毕！";
            $dictModel->updateCache(); //更新字典缓存
            $arr[] = "字典缓存[dict]更新完毕！";
            $shopExpressModel->updateCache(); //更新快递公司缓存
            $arr[] = "快递公司缓存[express]更新完毕！";
            $configModel->updateCache(); //更新配置缓存
            $arr[] = "站点配置缓存[webconfig]更新完毕！";
            $cmsCategoryModel->updateCache(); //更新cms栏目缓存
            $arr[] = "cms栏目缓存[cms_category}]更新完毕！";
            $goodsCategoryModel->updateCache(); //更新商品类目缓存
            $arr[] = "商品类目缓存[goods_category]更新完毕！";
        }
        $authRuleModel      = new AuthRuleModel();
        $authRuleModel->updateCache($this->getUtype()); //更新权限规则缓存
        $arr[] = "规则缓存[auth_rule_{$this->getUtype()}]更新完毕！";
        $this->assign('arr', $arr);
        return $this->fetch();
    }

    /**
     * 退出登录
     */
    public function exitLogin()
    {
        cookie($this->getUtype() . 'Des', null);
        $this->success('退出登录成功！', url('Public/login'));
    }

}
